package com.bj58.data.emailreports.controllers;

import java.io.File;
import java.io.FileInputStream;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import com.bj58.data.emailreports.model.SearchKWReportVo;
import com.bj58.data.emailreports.service.KWDataService;
import com.bj58.data.emailreports.utils.DateUtil;
import com.bj58.data.emailreports.utils.ResourcesUtil;
import com.bj58.data.emailreports.utils.TemplateMethodFactory;
import com.bj58.data.emailreports.utils.VelocityTools;
import com.bj58.groupbuy.service.locator.ServiceLocator;
import com.bj58.groupbuy.util.PathUtil;

public class KWDataController {
	private KWDataService kwService = ServiceLocator.get(KWDataService.class);
	
	
	/**搜索关键词周报 
	 * @param searchDate  发送的日期
	 * @param productType   1:黄页  2:招聘  3:房产  4:二手市场  5:二手车 6.其他
	 * @return
	 * @throws Exception
	 */
	public String searchKeyWordWeekReport(String searchDate,int productType)throws Exception{
		try {
			//velocity对象
			VelocityContext context = new VelocityContext();
			Object tools = TemplateMethodFactory.getInstance(VelocityTools.class);
			context.put("tools",tools);
			//读取模板内容
			String filepath =PathUtil.getWFNamespaceFolder()+"/emailtemp/SearchKeyWordReport.html";
			ResourcesUtil.setCharset(Charset.forName("utf-8"));
			FileInputStream inputStream = new FileInputStream(new File(filepath));
			String templateContext = ResourcesUtil.readString2(inputStream);
			inputStream.close();
			
			Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
			String beginDate = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 7),"yyyy-MM-dd");//查询开始日期(上周日)
			String endDate = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 1),"yyyy-MM-dd");//查询开始日期(上周1)
			
			//热门词
			List<SearchKWReportVo> hotList = kwService.searchKeyWordData(beginDate, endDate, productType, 1);
			context.put("hotList",hotList);
			//高频词
			List<SearchKWReportVo> hightList = kwService.searchKeyWordData(beginDate, endDate, productType, 2);
			context.put("hightList",hightList);
			//中低频词
			List<SearchKWReportVo> mlList = kwService.searchKeyWordData(beginDate, endDate, productType, 3);
			context.put("mlList",mlList);
			
			//站外搜索词
			List<SearchKWReportVo> zwList = kwService.searchKeyWordData(beginDate, endDate, productType, 4);
			context.put("zwList",zwList);
			
			if(null == hotList || hotList.isEmpty() || null == hightList || hightList.isEmpty() || null == mlList || mlList.isEmpty()){
				return "";
			}
			
			context.put("beginDate",beginDate);
			context.put("endDate",endDate);
			
			VelocityEngine velocity  = new VelocityEngine();
			Velocity.setProperty("input.encoding", "utf-8");
			Velocity.setProperty("output.encoding", "utf-8");
			String content = templateContext;
			StringWriter out = new StringWriter();
			try {
					velocity.evaluate(context, out, "", content);
			} catch (Exception e) {            
				e.printStackTrace();
			}
			content = out.toString();
			out.close();
			return content;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
}
